package com.aiyige.page.my.order.refund;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.eventbus.EventRefundReject;
import com.aiyige.model.OrderEntity;
import com.aiyige.model.RefundRejectModel;
import com.aiyige.model.request.RefundRejectRequest;
import com.aiyige.utils.ErrorUtil;
import com.aiyige.utils.InputNumFilter;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.TextWatcherAdapter;
import com.aiyige.utils.ToastX;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.aiyige.page.my.order.refund.RefundApplyForPage.MAX_REFUND_REASON_INPUT_NUM;

/**
 * 卖家处理驳回界面
 */
@Route(path = ARouterConfig.RefundRejectPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class RefundRejectPage extends RefundBaseActivity {




    @Autowired
    OrderEntity orderEntity = null;

    @BindView(R.id.et_content)
    EditText etContent;

    @BindView(R.id.tv_input_count)
    TextView tv_input_count;

    String rejectDescription = "";
    InputNumFilter inputNumFilter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.refund_reject_page;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ARouter.getInstance().inject(this);
        super.onCreate(savedInstanceState);
        init();
//        request();
    }

    @Override
    protected OrderEntity getOrderEntity() {
        return orderEntity;
    }

    private void init() {
        initTitleLayout(getString(R.string.reject_refund_request));
        inputNumFilter = new InputNumFilter(MAX_REFUND_REASON_INPUT_NUM);
        inputNumFilter.setListener(new InputNumFilter.Listener() {
            @Override
            public void inputNum(int num) {
                tv_input_count.setText(StringUtils.getString(R.string.input_num_count, num, MAX_REFUND_REASON_INPUT_NUM));
            }
        });
        etContent.setFilters(new InputFilter[]{inputNumFilter});
        etContent.addTextChangedListener(new TextWatcherAdapter() {
            @Override
            public void afterTextChanged(Editable s) {
                rejectDescription = s.toString();
            }
        });
    }


    private void request() {
        showLoading();
        RefundRejectRequest.newBuilder().id(orderEntity.getRefundId()).refundRejectModel(RefundRejectModel.newBuilder().rejectDescription(rejectDescription).build()).build().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                hideLoading();
                try {
                    if (!response.isSuccessful()) {
                        throw new Exception(ErrorUtil.extractMessageFromErrorResponse(response));
                    }
                    ToastX.show(getString(R.string.rejected));
                    EventBus.getDefault().post(new EventRefundReject());
                    finish();
                } catch (Exception e) {
                    onFailure(call, e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                hideLoading();
                if (t != null && !TextUtils.isEmpty(t.getMessage())) {
                    ToastX.show(t.getMessage());
                }
            }
        });
    }


    @OnClick({R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_submit:
                request();
                break;
        }
    }


}
